home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / TMT5.CPP < prev    next >
C/C++ Source or Header  |  1995-01-16  |  3KB  |  109 lines

  1.  
  2. //#define WANT_STREAM
  3.  
  4.  
  5. #include "include.h"
  6.  
  7. #include "newmat.h"
  8.  
  9.  
  10. /**************************** test program ******************************/
  11.  
  12. void Print(const Matrix& X);
  13. void Print(const UpperTriangularMatrix& X);
  14. void Print(const DiagonalMatrix& X);
  15. void Print(const SymmetricMatrix& X);
  16. void Print(const LowerTriangularMatrix& X);
  17.  
  18. ReturnMatrix Returner1(const GenericMatrix& GM)
  19. { Matrix M = GM+1; M.Release(); return M; }
  20.  
  21. ReturnMatrix Returner2(const GenericMatrix& GM)
  22. { UpperBandMatrix M = GM*2; M.Release(); return M; }
  23.  
  24. ReturnMatrix Returner3(const GenericMatrix& GM)
  25. { LowerBandMatrix M = GM*3; M.Release(); return M; }
  26.  
  27. ReturnMatrix Returner4(const GenericMatrix& GM)
  28. { SymmetricMatrix M = GM+4; M.Release(); return M; }
  29.  
  30. ReturnMatrix Returner5(const GenericMatrix& GM)
  31. { SymmetricBandMatrix M = GM*5; M.Release(); return M; }
  32.  
  33. ReturnMatrix Returner6(const GenericMatrix& GM)
  34. { BandMatrix M = GM*6; M.Release(); return M; }
  35.  
  36. ReturnMatrix Returner7(const GenericMatrix& GM)
  37. { DiagonalMatrix M = GM*7; M.Release(); return M; }
  38.  
  39. void trymat5()
  40. {
  41. //   cout << "\nFifth test of Matrix package\n";
  42.    Tracer et("Fifth test of Matrix package");
  43.    Exception::PrintTrace(TRUE);
  44.  
  45.    int i,j;
  46.  
  47.    Matrix A(5,6);
  48.    for (i=1;i<=5;i++) for (j=1;j<=6;j++) A(i,j)=1+i*j+i*i+j*j;
  49.    ColumnVector CV(6);
  50.    for (i=1;i<=6;i++) CV(i)=i*i+3;
  51.    ColumnVector CV2(5); for (i=1;i<=5;i++) CV2(i)=1.0;
  52.    ColumnVector CV1=CV;
  53.  
  54.    {
  55.       CV=A*CV;
  56.       RowVector RV=CV.t(); // RowVector RV; RV=CV.t();
  57.       RV=RV-1.0;
  58.       CV=(RV*A).t()+A.t()*CV2; CV1=(A.t()*A)*CV1 - CV;
  59.       Print(CV1);
  60.    }
  61.  
  62.    CV1.ReDimension(6);
  63.    CV2.ReDimension(6);
  64.    CV.ReDimension(6);
  65.    for (i=1;i<=6;i++) { CV1(i)=i*3+1; CV2(i)=10-i; CV(i)=11+i*2; }
  66.    ColumnVector CX=CV2-CV; { CX=CX+CV1; Print(CX); }
  67.    Print(ColumnVector(CV1+CV2-CV));
  68.    RowVector RV=CV.t(); RowVector RV1=CV1.t();
  69.    RowVector R=RV-RV1; Print(RowVector(R-CV2.t()));
  70.  
  71. // test loading of list
  72.  
  73.    RV.ReDimension(10);
  74.    for (i=1;i<=10;i++) RV(i) = i*i;
  75.    RV1.ReDimension(10);
  76.    RV1 << 1 << 4 << 9 << 16 << 25 << 36 << 49 << 64 << 81 << 100; // << 121;
  77.    Print(RowVector(RV-RV1));
  78.    et.ReName("Fifth test of Matrix package - almost at end");
  79.    Matrix X(2,3);
  80.    X << 11 << 12 << 13
  81.      << 21 << 22 << 23;
  82.    X(1,1) -= 11; X(1,2) -= 12; X(1,3) -= 13;
  83.    X(2,1) -= 21; X(2,2) -= 22; X(2,3) -= 23;
  84.    Print(X);
  85.  
  86.    et.ReName("Fifth test of Matrix package - at end");
  87.    RV = Returner1(RV)-1; Print(RowVector(RV-RV1));
  88.    CV1 = Returner1(RV.t())-1; Print(ColumnVector(RV.t()-CV1));
  89.    nricMatrix AA = A;
  90.    X = Returner1(AA)-A-1; Print(X);
  91.    UpperTriangularMatrix UT(31);
  92.    for (i=1; i<=31; i++) for (j=i; j<=31; j++) UT(i,j) = i+j+(i-j)*(i-2*j);
  93.    UpperBandMatrix UB(31,5); UB.Inject(UT);
  94.    LowerTriangularMatrix LT = UT.t();
  95.    LowerBandMatrix LB(31,5); LB.Inject(LT);
  96.    A = Returner2(UB).t()-LB*2; Print(A);
  97.    A = Returner3(LB).t()-UB*3; Print(A);
  98.    SymmetricMatrix SM; SM << (UT+LT);
  99.    A = Returner4(SM)-UT-LT-4; Print(A);
  100.    SymmetricBandMatrix SB(31,5); SB.Inject(SM);
  101.    A = Returner5(SB)/5-UB-LB; Print(A);
  102.    BandMatrix B = UB+LB*LB; A = LB;
  103.    A = Returner6(B)/6 - UB - A*A; Print(A);
  104.    DiagonalMatrix D; D << UT;
  105.    D << (Returner7(D)/7 - UT); Print(D);
  106.  
  107. //   cout << "\nEnd of fifth test\n";
  108. }
  109.